package com.iteaj.iot.test.mqtt;

import cn.hutool.json.JSONUtil;
import com.iteaj.iot.Message;
import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.client.mqtt.MqttClientException;
import com.iteaj.iot.client.mqtt.impl.MqttDefaultClientMessage;
import com.iteaj.iot.client.mqtt.message.MqttMessageHead;
import com.iteaj.iot.client.protocol.ServerInitiativeProtocol;
import com.iteaj.iot.test.ClientSnGen;
import com.iteaj.iot.test.TestConst;
import com.iteaj.iot.test.TestProtocolType;
import com.iteaj.iot.utils.ByteUtil;
import io.netty.handler.codec.mqtt.MqttQoS;

import java.nio.charset.StandardCharsets;

/**
 * mqtt订阅测试协议
 * @see com.iteaj.iot.client.mqtt.impl.MqttDefaultClientComponent 测试默认实现的Mqtt组件
 */
public class MqttSubscribeTestProtocol extends ServerInitiativeProtocol<MqttDefaultClientMessage> {

    public MqttSubscribeTestProtocol(MqttDefaultClientMessage requestMessage) {
        super(requestMessage);
    }

    @Override
    protected void doBuildRequestMessage(MqttDefaultClientMessage requestMessage) {
        Message.MessageHead head = requestMessage.getHead();
        TestProtocolType type = head.getType();

        // 遗嘱主题订阅
        if(type == TestProtocolType.WillTop) {
            byte[] message = requestMessage.getMessage();
            logger.info(TestConst.LOGGER_PROTOCOL_DESC, "mqtt", "遗嘱测试["+new String(message)+"]"
                    , head.getEquipCode(), head.getMessageId(), "通过");
        } else {
            String topic = requestMessage.getTopic();
            switch (topic) {
                case MqttClientTestHandle.EXACTLY_ONCE_TOPIC:
                    logger.info(TestConst.LOGGER_PROTOCOL_DESC, "mqtt协议", MqttQoS.EXACTLY_ONCE
                            , head.getEquipCode(), head.getMessageId(), "通过");
                    break;
                case MqttClientTestHandle.AT_LEAST_ONCE_TOPIC:
                    logger.info(TestConst.LOGGER_PROTOCOL_DESC, "mqtt协议", MqttQoS.AT_LEAST_ONCE
                            , head.getEquipCode(), head.getMessageId(), "通过");
                    break;
                case MqttClientTestHandle.AT_MOST_ONCE_TOPIC:
                    logger.info(TestConst.LOGGER_PROTOCOL_DESC, "mqtt协议", MqttQoS.AT_MOST_ONCE
                            , head.getEquipCode(), head.getMessageId(), "通过");
                    break;
                default:
                    throw new MqttClientException("不支持的topic");
            }
        }
    }

    @Override
    protected MqttDefaultClientMessage doBuildResponseMessage() {
        MqttMessageHead head = requestMessage().getHead();
        if(head.getType() != TestProtocolType.WillTop) {
            MqttMessageHead messageHead = new MqttMessageHead(head.getEquipCode(), head.getMessageId(), TestProtocolType.CIReq);
            messageHead.setMessage(JSONUtil.toJsonStr(messageHead).getBytes(StandardCharsets.UTF_8));
            return new MqttDefaultClientMessage(messageHead, MqttQoS.AT_MOST_ONCE, MqttClientTestHandle.TOPIC_RESPONSE+"/"+head.getEquipCode());
        }

        return null;

    }

    @Override
    public ProtocolType protocolType() {
        return TestProtocolType.PIReq;
    }
}
